package com.kinth.dbutil;


import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	private static C3P0Utils dbcputils = null;
	private ComboPooledDataSource cpds = null;

	private static final Logger LOGGER = Logger.getLogger(C3P0Utils.class);

	private C3P0Utils() {
		if (cpds == null) {
			cpds = new ComboPooledDataSource("mysql");
		}
		System.out.println("自动增长"+cpds.getAcquireIncrement());
		System.out.println("自动增长"+cpds.getUser());
	
//		cpds.setUser("data_user");
//		cpds.setPassword("data12345!");
//		cpds.setJdbcUrl("jdbc:mysql://rds85yhg33trtgtpkwacnpublic.mysql.rds.aliyuncs.com:3306/data_db");
//		try {
//			cpds.setDriverClass("com.mysql.jdbc.Driver");
//		} catch (PropertyVetoException e) {
//			e.printStackTrace();
//		}
//		cpds.setInitialPoolSize(5);
//		cpds.setMaxIdleTime(60);
//		cpds.setMaxPoolSize(20);
//		cpds.setMinPoolSize(3);
//		cpds.setCheckoutTimeout(20000);
//		cpds.setMaxStatements(100);
//		cpds.setIdleConnectionTestPeriod(100);
//		cpds.setAutoCommitOnClose(true);
//		cpds.setAcquireIncrement(3);
//		cpds.setAcquireRetryAttempts(5);
//		cpds.setAcquireRetryDelay(1000);
		
	}

	public synchronized static C3P0Utils getInstance() {
		if (dbcputils == null)
			dbcputils = new C3P0Utils();
		if(dbcputils == null)
			System.out.println("空");
		else
			System.out.println("非空");
		return dbcputils;
	}

	public static void releaseConnection(Connection conn)
	{
		try
		{
			conn.close();
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Connection getConnection() {
		Connection con = null;
		try {
			con = cpds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}

	 

	public static void main(String[] args) throws SQLException {
		System.out.println("测试：");
		Connection con = null;
		long begin = System.currentTimeMillis();
		for (int i = 0; i < 100; i++) {
			con = C3P0Utils.getInstance().getConnection();
			LOGGER.info(con);
			System.out.println("con: "+con);
			con.close();
		}
		long end = System.currentTimeMillis();
		System.out.println(":" + (end - begin) + "ms");
	}
}